package com.airtight.game;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;

import com.airtight.screens.ScreenManager;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;


class GameRenderer implements Renderer {

    //private int fps = 60;
    //private float interval = 1000/fps;
    //int count = 0;
    private Context context;
    private ScreenManager screenManager;
    private GameState gameState;
   // private long millis = System.currentTimeMillis();

    GameRenderer(Context context, GameState gameState) {
        this.context = context;
        this.gameState = gameState;
    }

    ScreenManager getScreenManager(){
        return screenManager;
    }

    /**Called when the screen is first created**/
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        screenManager = new ScreenManager(context, gameState);
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
        GLES20.glEnable(GLES20.GL_BLEND);
        Log.d("Renderer", "Surface Created");
    }

    /**Called when the screen changes orientation**/
    @Override
    public void onSurfaceChanged(GL10 gl10, int width, int height) {
        GLES20.glViewport(0,0,width,height);
        float ratio = (float) height/ (float) width;
        screenManager.getScreen().changeSurface(ratio);
        Log.d("Renderer", "Surface Changed");
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        glClear(GL_COLOR_BUFFER_BIT);
        screenManager.getScreen().draw();
        screenManager.getScreen().update();
        //float delta = System.currentTimeMillis() - millis;
    }
}